package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dto.ChannelDto;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>
    implements AdChannelService {
  @Override //频道分页列表查询
  public ResponseResult findByNameAndPage(ChannelDto dto) {
    // 1.参数检测
    dto.checkParam();
    // 2.按名称模糊分页查询
    IPage<AdChannel> page = new Page(dto.getPage(), dto.getSize());

    LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

    if (StringUtils.isNotBlank(dto.getName())) {
      lambdaQueryWrapper.like(AdChannel::getName, dto.getName());
    }
    // 执行查询得到分页结果
    IPage result = page(page, lambdaQueryWrapper);

    // 3.封装PageResonseResult
    ResponseResult responseResult =
        new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
    responseResult.setData(result.getRecords());
    return responseResult;
  }

  /**
   * 新增
   *
   * @param adChannel 管理员
   * @return
   */
  @Override
  public ResponseResult insert(AdChannel adChannel) {

    // 1 参数校验
//    if (StringUtils.isBlank(adChannel.getName())) {
//      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
//    }
    // 2 执行新增
    adChannel.setCreatedTime(new Date());
    this.save(adChannel);
    // 3 返回结果
    return ResponseResult.okResult();
  }

  /**
   * 频道修改
   *
   * @param adChannel
   * @return 页面响应
   */
  @Override
  public ResponseResult amend(AdChannel adChannel) {
//    // 1.检查参数
//    if (null == adChannel || adChannel.getId() == null) {
//      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//    }
    // 先查后改
    AdChannel channel = this.getById(adChannel.getId());
    if (null == channel) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 2.修改
    this.updateById(adChannel);
    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
  }

  /**
   * 根据id删除频道
   *
   * @param id
   * @return 返回成功或失败即可
   */
  @Override
  public ResponseResult deleteById(Integer id) {

    // 1.检查参数
    if (null == id) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }

    // 2.判断当前频道是否存在 和 是否有效
    AdChannel channel = this.getById(id);
    if (null == channel) {
      return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }
    // 启用状态下不能删除
    if (channel.getStatus()) {
      return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    // 3.删除频道
    this.removeById(id);

    return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
  }
}
